package com.study.sxc.controller;

import com.study.sxc.Entity.User;
import com.study.sxc.enums.ResponseCode;
import com.study.sxc.response.Response;
import com.study.sxc.service.userService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@ResponseBody
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private userService userService;

    @GetMapping("/get")
    public Response<List<User>> getUsers() {
        List<User> users;
        try {
            users = userService.getAllUsers();
        } catch (Exception e) {
            return Response.<List<User>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info("查询异常: " + e.getMessage())
                    .data(null)
                    .build();
        }
        if(users == null || users.isEmpty()) {
            return Response.<List<User>>builder()
                    .code(ResponseCode.EMPTY_RESULT.getCode())
                    .info(ResponseCode.EMPTY_RESULT.getInfo())
                    .data(users)
                    .build();
        }
        return Response.<List<User>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(users)
                .build();
    }

    @PostMapping("/add")
    public Response<Void> addUser(@RequestBody User user) {
        // 校验必要字段
        if (user.getName() == null || user.getName().trim().isEmpty()) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("用户名不能为空")
                    .build();
        }
        int result = userService.addUser(user);
        if (result > 0) {
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } else {
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @PutMapping("/update")
    public Response<Void> updateUser(@RequestBody User user) {
        // 校验必要字段
        if (user.getUserId() == null || user.getUserId() <= 0) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("用户ID不能为空且必须为正整数")
                    .build();
        }
        int result = userService.updateUser(user);
        if (result > 0) {
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } else {
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @DeleteMapping("/delete")
    public Response<Void> deleteUser(@RequestBody User user) {
        Integer id = user.getUserId();
        if (id == null || id <= 0) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("用户ID不能为空且必须为正整数")
                    .build();
        }
        userService.deleteUser(id);
        return Response.<Void>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .build();
    }

    @GetMapping("/get/{id}")
    public Response<User> getUserById(@PathVariable Integer id) {
        // 根据id获取用户信息
        User user = userService.getUserById(id);
        if (user == null) {
            return Response.<User>builder()
                    .code(ResponseCode.EMPTY_RESULT.getCode())
                    .info(ResponseCode.EMPTY_RESULT.getInfo())
                    .data(null)
                    .build();
        }
        return Response.<User>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(user)
                .build();
    }
} 